package com.example.demo.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * @Author: zjq
 * @Date: create in 2023/3/22 14:40
 */
@Slf4j
public class MysqlEncryptUtil {
    /**
     * SQL示例
     * SELECT AES_DECRYPT(UNHEX(idcard), 'ihaierForTodoKey') FROM student WHERE id =1
     * SELECT HEX(AES_ENCRYPT(123, 'ihaierForTodoKey'))
     */

    //秘钥设置16位
    private static final String SECRET_KEY = "ihaierForTodoKey";
    //加密方式
    private static final String AES_TYPE = "AES";
    //秘钥最大长度16位
    private static final int BYTE_LENGTH = 16;

    /**
     * 加密AES
     *
     * @param value 字符串
     * @param key   秘钥
     * @return String
     */
    private static String encryptAES(String key, String value) {
        try {
            byte[] keyBytes = Arrays.copyOf(key.getBytes(StandardCharsets.US_ASCII), BYTE_LENGTH);
            SecretKey keyStr = new SecretKeySpec(keyBytes, AES_TYPE);
            Cipher cipher = Cipher.getInstance(AES_TYPE);
            cipher.init(Cipher.ENCRYPT_MODE, keyStr);
            byte[] cleartext = value.getBytes(StandardCharsets.UTF_8);
            byte[] ciphertextBytes = cipher.doFinal(cleartext);
            return new String(Hex.encodeHex(ciphertextBytes)).toUpperCase();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * 解密AES
     *
     * @param encrypted 字符串
     * @param key       秘钥
     * @return String
     */
    private static String decryptAES(String key, String encrypted) {
        try {
            byte[] keyBytes = Arrays.copyOf(key.getBytes(StandardCharsets.US_ASCII), BYTE_LENGTH);
            SecretKey keyStr = new SecretKeySpec(keyBytes, AES_TYPE);
            Cipher cipher = Cipher.getInstance(AES_TYPE);
            cipher.init(Cipher.DECRYPT_MODE, keyStr);
            byte[] content = Hex.decodeHex(encrypted.toCharArray());
            byte[] ciphertextBytes = cipher.doFinal(content);
            return new String(ciphertextBytes);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * 加密
     *
     * @param value 字符串
     * @return String
     */
    public static String encrypt(String value) {
        return StringUtils.isEmpty(value) ? null : encryptAES(SECRET_KEY, value.trim());
    }

    /**
     * 解密
     *
     * @param value 字符串
     * @return String
     */
    public static String decrypt(String value) {
        if (StringUtils.isNotEmpty(value)) {
            String value2 = decryptAES(SECRET_KEY, value.toLowerCase());
            value = StringUtils.isEmpty(value2) ? value : value2;
        }
        return value;
    }

    /**
     * main方法
     *
     * @param args 参数
     */
    public static void main(String[] args) {
        //加密测试
        System.out.println(encrypt("张三"));
        //解密测试
        System.out.println(decrypt(encrypt("张三")));

        //加密测试
        System.out.println(encrypt("李四"));
        //解密测试
        System.out.println(decrypt(encrypt("李四")));
    }
}
